package activity;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

/**
 * Created by mary on 2017/7/13.
 */

public class SendEmail {
    int number;
    public int RandomNumber(){
        number=(int)((Math.random()*9+1)*100000);
        return number;
    }
    public SendEmail(String to) throws MessagingException{
        final Properties properties=new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.user", "470465652@qq.com");
        properties.put("mail.password","vxpelcqfpkqbcbaf");
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = properties.getProperty("mail.user");
                String password = properties.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(properties, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(
                properties.getProperty("mail.user"));
        message.setFrom(form);

        // 设置收件人的邮箱
        InternetAddress toAddress = new InternetAddress(to);
        message.setRecipient(RecipientType.TO, toAddress);

        // 设置邮件标题
        message.setSubject("ci_helper验证邮件");

        // 设置邮件的内容体
        message.setContent("这是一封验证邮件，您的验证码为"+number+"请将验证码输入您", "text/html;charset=UTF-8");

        // 最后当然就是发送邮件啦
        Transport.send(message);

    }
}
